<?php


namespace app\common\library;


use common;
use hiredis;
use think\facade\Log;

class AccessTokenModel {

    function refresh_wx_access_token($appid) {
        $platform_info = (new PlatformModel())->get_platform_info($appid);
        if (empty($platform_info)) {
            Log::debug("AccessTokenModel get platform info error, appid=" . $appid);
        }

        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$platform_info['appid']}&secret={$platform_info['key']}";
        $result = common::curlgetjson($url, []);
        if ($result && isset($result['access_token'])) {
            $info = [
                'access_token' => $result['access_token'],
                'expire' => time() + $result['expires_in'],
            ];

            $key = "WX_ACCESS_TOKEN|" . $appid;
            hiredis::db()->hMset($key, $info);
            return [$result['access_token'], $result['expires_in']];
        }
        return null;
    }

    function refresh_wx_access_token_from_7yu($appid) {
        $platform_info = (new PlatformModel())->get_platform_info($appid);
        if (empty($platform_info)) {
            Log::debug("AccessTokenModel get platform info error, appid=" . $appid);
        }

        $time = time();

        $params = [
            'appid' => $platform_info['appid'],
            'secret' => $platform_info['key'],
        ];

        $nonce = md5(json_encode($params));

        $checkSum = bin2hex(sha1($platform_info['key'] . $nonce . $time));

        $url = "https://qiyukf.com/openapi/wechat/miniprogram/access_token?appKey=56c4e017ad9be060ccd2c0cb099b479f&time={$time}&checksum={$checkSum}";
        $result = common::curlpostjson($url, $params);

        if ($result && isset($result['code']) && $result['code'] == 200) {
            if (isset($result['message'])) {
                $j = json_decode($result['message']);
                if ($j && isset($j['access_token']) && isset($j['expires_in'])) {

                    $info = [
                        'access_token' => $j['access_token'],
                        'expire' => time() + $j['expires_in'],
                    ];

                    $key = "WX_ACCESS_TOKEN|" . $appid;
                    hiredis::db()->hMset($key, $info);
                    return $j['access_token'];
                }
            }
        }
        return null;
    }

    function get($appid) {
        $key = "WX_ACCESS_TOKEN|" . $appid;
        $kvs = hiredis::db()->hGetAll($key);
        if (empty($kvs) || intval($kvs['expire'] - time()) <= 0) {
            return $this->refresh_wx_access_token($appid);
        } else {
            if (intval($kvs['expire'] - time()) < 300) {
                $params = [
                    'appid' => $appid
                ];
                TimerALModel::instance()->call_bus_server('/inner/refresh_wx_access_token', $params, 0);
            }
            return [$kvs['access_token'], intval($kvs['expire']) - time()];
        }
    }

}
